﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MaxLeaf.Platform.Contract;
using MaxLeaf.Platform.DataContract;
using CommonsLib.DB;
using System.Transactions;
using System.Data.SqlClient;
using System.Data.Common;
using System.Diagnostics;
using CommonsLib.Settings;
using CommonsLib.Utility;
using System.Data;

using System.Security.Cryptography;
using MaxLeaf.Platform.Functions;
using MaxLeaf.Platform.Commons;

namespace MaxLeaf.Platform.DALImplementation
{
    public class imStoresOutputHdrDAL : BaseDAL
    {
       
        //public struct DtlSumData
        //{
        //    public int GoodsTypeCount;
        //    public int GoodsQtyCount;
        //    public double  TotAmt;
        //}


        //public DtlSumData GetDtlSumData(CommonsLib.DB.DBAccess dbcon, DataContract.imStoresOutputHdrDataModel hdrData)
        //{
        //    object hdrKeyValue=DataFunc.GetKeyValue(hdrData);
        //    DtlSumData sumData = new DtlSumData( );
        //    string strSql = "select count(*)  from (select distinct GoodsID,GoodsAttrID from imStoresOutputDtl where HdrID=@FInterID) a ";
        //    sumData.GoodsTypeCount = CvtFunc.ToInt(dbcon.GetValue(strSql, new SqlParameter[] { new SqlParameter("@FInterID", hdrKeyValue) }));

        //    strSql = "select isnull(sum(ReqQty),0) as ReqQty,isnull(sum(Subtotal),0) as Subtotal  from  imStoresOutputDtl where HdrID=@FInterID  ";
        //    using (DbDataReader rdr = dbcon.GetDataFirstRow(strSql, new SqlParameter[] { new SqlParameter("@FInterID", hdrKeyValue) }))
        //    {
        //        if(rdr.Read())
        //        {
        //            sumData.GoodsQtyCount =CvtFunc.ToInt( rdr.GetValue(0));
        //            sumData.TotAmt = CvtFunc.ToDouble(rdr.GetValue(1));

        //        }
        //    }
        //    return sumData;
        //}



        public bool InitStoreOutDtl(imStoresOutputDtlDataModel imStoreOutDtlDataModel)
        {
            imStoresOutputDtlDataModel imDataModel = (imStoresOutputDtlDataModel)imStoreOutDtlDataModel;
            return true;
            
        }
        
        public void SetPosted(CommonsLib.DB.DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update imStoresOutputHdr set posted=1  where FInterID=@FInterID and posted=0 
                and not exists (select hdrid from imStoresOutputDtl where HdrID=@FInterID and posted=0) ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }

        public void SetUnPosted(DBAccess dbcon, int intHdrID)
        {
            string strSql = @"update imStoresOutputHdr set posted=0  where FInterID=@FInterID and posted=1 
                and   exists (select hdrid from imStoresOutputDtl where HdrID=@FInterID and posted=0  and is_delete=0) ";
            dbcon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FInterID", intHdrID.ToString())
 
            });
        }
    }
}
